package com.slashmanx.helpers;

import java.io.IOException;

import com.slashmanx.socialwall.SocialWallActivity;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

public class Utilities
{
	public static int getRandomNumber(int min, int max) 
	{
        return (int)((Math.random() * (max - min)) + min);
	}
	
	public static String getFileNameFromURL(String url)
	{
		int slashIndex = url.lastIndexOf('/');
	    return url.substring(slashIndex + 1);
	}
	
	public static boolean isOnline(Context ctx) 
	{
	    ConnectivityManager connectivityManager  = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
	    return activeNetworkInfo != null;

	}
	
	public static void setWallpaper(Activity activity, Context context, String filename)
	{
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(context);
        final int fullWidth = myWallpaperManager.getDesiredMinimumWidth();
        final int fullHeight = myWallpaperManager.getDesiredMinimumHeight();

		   Log.i("HELLO", "w="+fullWidth+" h="+fullHeight); 

	    Bitmap wp = BitmapFactory.decodeFile(filename);
	    
	   DisplayMetrics dm = new DisplayMetrics();
	   activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
	  //  double ratio = wp.getHeight() / wp.getWidth();
	   int newHeight = dm.heightPixels / 2;//getStatusBarHeight(activity);
	   int newWidth = dm.widthPixels * 2;
	    //myWallpaperManager.suggestDesiredDimensions(myWallpaperManager.getDesiredMinimumWidth(), wp.getHeight()-38);
	    Bitmap swp = Bitmap.createScaledBitmap(wp, fullWidth, fullHeight, false);
	    
	    
        try 
        {
        		//myWallpaperManager.setWallpaperOffsetSteps(0f, 0f);
                myWallpaperManager.setBitmap(swp);   
                Toast.makeText(context, "Wallpaper Set", Toast.LENGTH_SHORT).show();

        } 
        catch (IOException e) 
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }

	}
	
	public static int getStatusBarHeight(Activity activity)
	{
		Rect rectgle= new Rect();
		Window window= activity.getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
		int StatusBarHeight= rectgle.top;
		int contentViewTop= 
		    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
		int TitleBarHeight= contentViewTop - StatusBarHeight;

		   Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight); 

		return TitleBarHeight * 2;
	}


}
